<?php
class classImageViewer extends phpajax {
	private $container_name;
	private $show_navigation;
	
	function __construct($container_name='flow_container',$show_navigation=false){
		$this->container_name = $container_name;
		$this->show_navigation= $show_navigation;
	}
	
	function input(){
		aread("Show_Product_Image"); //complete path to image
        aread("Show_With_Navigation");//request to show navigation buttons from ajax
        aread("Show_Product_ID"); //ID of showed product
		aread("Close_Product_Image");
		aread("Image_Show_As_Child");//boolean value - if it is 1, after click on close button, shadow wont be closed
	}
	
	function loading() {} 
	
	function main() {
		$iImgPath 	= & $this->Show_Product_Image;
		$iImgClose 	= & $this->Close_Product_Image;
		$lImage_Show_As_Child = & $this->Image_Show_As_Child;
        $iPID       = $this->Show_Product_ID;
		
		$_Window = new classWindow();
                
        if($this->Show_With_Navigation=='true')
            $this->show_navigation=true;
        else{
            if($iPID!='' and is_numeric($iPID) and $iPID>0){//show navigation, if product has two or more images
                $iPCount = 0;
                if(file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$iPID._IMAGES_FILE_TYPE_)) $iPCount++;
                if(file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$iPID.'d2'._IMAGES_FILE_TYPE_)) $iPCount++;
                if(file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$iPID.'d3'._IMAGES_FILE_TYPE_)) $iPCount++;
                if($iPCount>1) $this->show_navigation=true;
            }
        }
		
		//test if image exists
		if (!file_exists($iImgPath)){
			aprint($this->container_name, $_Window->ShowByAJAX($this->container_name,'<br><br>Obrázok nenájdený:'.$iImgPath));
		}
		//show image - NOT as child window
		if (($iImgPath!='') and ($iImgClose!=1) and ($lImage_Show_As_Child!='1')){
			aprint($this->container_name,$_Window->ShowByAJAX($this->container_name,$this->getImageForm($iImgPath,$this->show_navigation,$iPID)));
		}
		//show image - like child window
		if (($iImgPath!='') and ($iImgClose!=1) and ($lImage_Show_As_Child=='1')){
			aprint($this->container_name,$_Window->ShowByAJAX($this->container_name,$this->getImageForm($iImgPath,$this->show_navigation,$iPID),true));
		}
		
		//close image
		if($iImgClose==1) {
			aprint($this->container_name, '');
		}
	}

	
	private function getImageForm($iImgPath, $bNavigation, $iPID){
        //create photoalbums description
        $dirname = dirname($iImgPath);
        $filename = basename($iImgPath);
        $_XMLService = new classImageViewerXMLService();
        $_XMLService->getXMLDataDOM($dirname,$filename,&$sImageName,&$sImageDescription);

        if(is_numeric($iPID) and $iPID>0) $PID_JS = 'document.getElementById(\'Show_Product_ID\').value = \''.$iPID.'\';';
        else $PID_JS = '';
                
		$_Skin = new classTemplate();		
		$result = '
			<div 
				style="border:0; width:100%" height:100%; background-color:transparent;
		';
		
		if($bNavigation==true)
			$result .='	
				onmouseover="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'visible\';
				"
				onmouseout="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'hidden\';
				"
			';
		
		$result .='
			>
			<img 
				id="imageviewer_image" 
				src="'.$iImgPath.'" 
				alt="image"
		';
			
		if($bNavigation==true)
			$result .='	
				onmouseover="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'visible\';
				"
				onmouseout="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'hidden\';
				"
			';
		
		$result .='
			>
		';
		
		if($bNavigation==true)
		$result .='		
			<img 
				id="imageviewer_navigator_control_container" 
				src="'.$_Skin->getActual().'/images/imgw_nav_container.png" 
				alt=""
				style="position:absolute; top:45px; left:0px;"
				onmouseover="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'visible\';
				"
				onmouseout="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'hidden\';
				"
			>
			<font
				id="imageviewer_navigator_control_prior"
				href="'._URL_INACTIVE_ADDR_.'"
				onclick="
					document.getElementById(\'Show_Product_Image\').value = \''.$this->getPathOfImage($iImgPath, $iPID, -1).'\';
                    '.$PID_JS.'
					classImageViewer();
				"
				onmouseover="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'visible\';
				"
				onmouseout="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'hidden\';
				"
				style="position:absolute; top:55px; left:0px; color:white; cursor:pointer;"
			>
				'._i18n_imageviewer_BACK_.'
			</font>
			
			<font
				id="imageviewer_navigator_control_next"
				href="'._URL_INACTIVE_ADDR_.'"
				onclick="
					document.getElementById(\'Show_Product_Image\').value = \''.$this->getPathOfImage($iImgPath, $iPID, 1).'\';
                    '.$PID_JS.'
					classImageViewer();
				"
				onmouseover="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'visible\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'visible\';
				"
				onmouseout="
					document.getElementById(\'imageviewer_navigator_control_prior\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_next\').style.visibility=\'hidden\';
					document.getElementById(\'imageviewer_navigator_control_container\').style.visibility=\'hidden\';
				"
				style="position:absolute; top:75px; left:0px; color:white; cursor:pointer;"
			>
				'._i18n_imageviewer_NEXT_.'
			</font>
		';
        if(trim($sImageName)!='' or trim($sImageDescription)!='')
            $result .='
                    <div style="width:300px; font-family:verdana; font-size:10px;">
                        <b>'.$sImageName.'</b> '.$sImageDescription.'
                    </div>
                </div>
            ';
		
		return $result;
	}
	
	private function getPathOfImage($iImgPath, $iPID, $Position){
        $iIndex 	= 0;
        $dirname 	= dirname($iImgPath);
        $filename	= basename($iImgPath);
        if(!is_dir($dirname)) return '';

        //create array of files
        if($iPID!='' and is_numeric($iPID) and $iPID>0){ //images of product
            $filesList = $this->getProductsImageList($iPID);
        }
        else{
            $_fileSystem = new classFileSystem();
            $filesList = $_fileSystem->getFilesList($dirname,array('jpg','jpeg','bmp','png','gif'));
        }

        //sort array of files
        array_multisort($filesList, SORT_STRING);

        //find actual position of filename from array of files name
        $iFIndex = array_search($filename,$filesList);

        if(($iFIndex+$Position)<0 or ($iFIndex+$Position)>(count($filesList)-1))
                return $dirname.'/'.$filesList[$iFIndex];

        return $dirname.'/'.$filesList[$iFIndex+$Position];
        //return $filename.'<br><br>'.implode('; <br>',$filesList).'<br><br>'.$dirname.'/'.$filesList[$iFIndex+$Position];
	}

    private function getProductsImageList($iPID){
        $result = ''; $iIndex=0;
        if($iPID!='' and is_numeric($iPID) and $iPID>0){//show navigation, if product has two or more images
            if(file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$iPID._IMAGES_FILE_TYPE_)) $result[$iIndex++]=$iPID._IMAGES_FILE_TYPE_;
            if(file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$iPID.'d2'._IMAGES_FILE_TYPE_)) $result[$iIndex++]=$iPID.'d2'._IMAGES_FILE_TYPE_;
            if(file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$iPID.'d3'._IMAGES_FILE_TYPE_)) $result[$iIndex++]=$iPID.'d3'._IMAGES_FILE_TYPE_;
        }
        return $result;
    }
}
?>